package com.qm.console.controller.rest;

import com.google.common.collect.ImmutableMap;
import com.qm.console.controller.dto.BaseRsp;
import com.qm.console.controller.dto.ResultEnum;
import com.qm.console.controller.vo.comm.query.TimeOffsetQuery;
import com.qm.console.dao.model.BxDisplayFieldDO;
import com.qm.console.pubdef.dto.bx.BxAtomFieldDTO;
import com.qm.console.pubdef.dto.bx.BxDisplayFieldDTO;
import com.qm.console.pubdef.dto.bx.BxJobDTO;
import com.qm.console.pubdef.dto.bx.BxRunJobDTO;
import com.qm.console.pubdef.dto.bx.UpdateDictQuery;
import com.qm.console.service.bx.BxDataService;
import com.qm.console.service.bx.BxService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

/**
 * //                       _oo0oo_
 * //                      o8888888o
 * //                      88" . "88
 * //                      (| -_- |)
 * //                      0\  =  /0
 * //                    ___/`---'\___
 * //                  .' \\|     |// '.
 * //                 / \\|||  :  |||// \
 * //                / _||||| -:- |||||- \
 * //               |   | \\\  -  /// |   |
 * //               | \_|  ''\---/''  |_/ |
 * //               \  .-\__  '-'  ___/-. /
 * //             ___'. .'  /--.--\  `. .'___
 * //          ."" '<  `.___\_<|>_/___.' >' "".
 * //         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * //         \  \ `_.   \_ __\ /__ _/   .-` /  /
 * //     =====`-.____`.___ \_____/___.-`___.-'=====
 * //                       `=---='
 * //
 * //
 * //     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * //
 * //               佛祖保佑         永无BUG
 * <p>
 * com.qm.console.controller.rest
 * Created on 2021/1/8.
 */
@RestController
@RequestMapping("/bx")
public class BxController {

    @Autowired
    private BxService bxService;
    @Autowired
    private BxDataService bxDataService;

    @GetMapping("/dict")
    public BaseRsp listDict() {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("list", bxService.listDict()));
    };

    @GetMapping("/dict/numeric")
    public BaseRsp listNumericDict() {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("list", bxService.listNumericDict()));
    }

    @PutMapping("/dict")
    public BaseRsp updateDictDesc(@RequestBody UpdateDictQuery query) {
        bxService.updateDictDisplay(query.getId(), query.getDesc());
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/jobs")
    public BaseRsp listJobs() {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("list", bxService.listJobs()));
    }

    @PostMapping("/job")
    public BaseRsp addJob(@RequestBody BxJobDTO jobDTO) {
        bxService.addJob(jobDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping("/job")
    public BaseRsp updateJob(@RequestBody BxJobDTO jobDTO) {
        bxService.updateJob(jobDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/job/{id}")
    public BaseRsp deleteJob(@PathVariable("id") Integer id) {
        bxService.deleteJob(id);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/job/once")
    public BaseRsp testJob(BxRunJobDTO query) {
        bxService.runJobOnce(query);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/job/online/{id}")
    public BaseRsp scheduleJob(@PathVariable("id") Integer id) {
        bxService.setJobOnline(id);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/job/offline/{id}")
    public BaseRsp offlineJob(@PathVariable("id") Integer id) {
        bxService.setJobOffline(id);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/atom_fields")
    public BaseRsp listAtomFields() {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("list", bxService.listAtomField()));
    }

    @PostMapping("/atom_field")
    public BaseRsp addAtomField(@RequestBody BxAtomFieldDTO atomFieldDTO) {
        bxService.addAtomField(atomFieldDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }


    @PutMapping("/atom_field")
    public BaseRsp updateAtomField(@RequestBody BxAtomFieldDTO atomFieldDTO) {
        bxService.updateAtomField(atomFieldDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/atom_field/{id}")
    public BaseRsp deleteAtomField(@PathVariable("id") Integer id) {
        bxService.deleteAtomField(id);
        return new BaseRsp(ResultEnum.SUCCESS);
    }


    @GetMapping("/display_fields")
    public BaseRsp listDisplayFields() {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("list", bxService.listDisplayField()));
    }

    @PostMapping("/display_field")
    public BaseRsp addDisplayField(@RequestBody BxDisplayFieldDTO displayFieldDO) {
        bxService.addDisplayField(displayFieldDO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PutMapping("/display_field")
    public BaseRsp updateDisplayField(@RequestBody BxDisplayFieldDTO displayFieldDO) {
        bxService.updateDisplayField(displayFieldDO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/display_field/{id}")
    public BaseRsp deleteDisplayField(@PathVariable("id") Integer id) {
        bxService.deleteDisplayField(id);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    /* ***********data part*********** */

    @GetMapping("/data/filters/{id}")
    public BaseRsp getJobFilters(@PathVariable("id") Integer id) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("list", bxDataService.getJobFilters(id)));
    }

    @GetMapping("/data/raw/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "limit", required = false, dataType = "int", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "offset", value = "offset", required = false, dataType = "int", paramType = "query", defaultValue = "0"),
    })
    public BaseRsp getJobRawData(@PathVariable("id") Integer id, TimeOffsetQuery query) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("list", bxDataService.getJobRawData(id, query),
                "count", bxDataService.getJobRawDataCount(id, query),
                "columns", bxDataService.getJobRawDataColumns(id)));
    }

    @GetMapping("/data/{id}")
    public BaseRsp getJobData(@PathVariable("id") Integer id, @RequestParam Map<String, String> requestParams) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of(
                "list", bxDataService.getJobData(id, requestParams),
                "count", bxDataService.getJobDataCount(id, requestParams),
                "columns", bxDataService.getJobDataColumns(id)));
    }

    @PutMapping("/pre/atom_field")
    public BaseRsp preUpdateAtomField(@RequestBody BxAtomFieldDTO atomFieldDTO) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("msg", bxService.preUpdateAtomField(atomFieldDTO)));
    }

    @PutMapping("/pre/job")
    public BaseRsp preUpdateJob(@RequestBody BxJobDTO job) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("msg", bxService.preUpdateJob(job)));
    }

    @PutMapping("/pre/display_field")
    public BaseRsp preUpdateDisplayField(@RequestBody BxDisplayFieldDTO displayFIeldDTO) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("msg", bxService.preUpdateDisplayField(displayFIeldDTO)));
    }



}
